// define the whole namespace for the logger application
ul = {};

// import the syntax highlighter
include( "../dp.SyntaxHighlighter/Scripts/shCore.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushAs2.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushAs3.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushCpp.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushCSharp.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushCss.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushDelphi.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushJava.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushJscript.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushPhp.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushPython.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushRuby.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushSql.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushVb.js" );
include( "../dp.SyntaxHighlighter/Scripts/shBrushXml.js" );

// import logger files
include( "../src/assert.js" );
include( "../src/collection.js" );
include( "../src/constants.js" );
include( "../src/collection.js" );
include( "../src/events.js" );
include( "../src/effects.js" );
include( "../src/filters.js" );
include( "../src/file.js" );
include( "../src/templates.js" );
include( "../src/format.js" );
include( "../src/log.js" );
include( "../src/tabs.js" );

function main ()
{
	exceptDetails = false;
	formatManager = new ul.Format.FormatManager();
	tabs = new ul.Tab.TabView( $( "tabs" ), $( "tabs-content" ), $("popup") );					
	//expandController = new ul.Log.ExpandController( $( "expand" ) );
	filterController = new ul.Filter.FilterController( $( "filters" ) );
	
	tabs.eventDispatcher.addListener( filterController );

	dp.SyntaxHighlighter.ClipboardSwf = '/flash/clipboard.swf';
}

function requestId ( name )
{
	var file = new ul.File ( FileIdFactory.getNextId(), name );
	tabs.addTab ( new ul.Tab.TabEntity( file ) );
	
	return file.getId();
}

function log ( id, msg )
{
	var html
	if( typeof( msg ) == "object" )
	{
		html = formatManager.getFormatter( ul.Format.LOG ).format( msg );
	} 
	else
	{
		var message = eval( "(" + msg +")" );
		html = formatManager.getFormatter( ul.Format.LOG ).format( message );
	}
	var tab = tabs.getTabById( id )
	var file = tab.getFile();
	
	file.setCache( file.getCache() + html );
	
	if( tab == tabs.tabUnderFocus )
		tabs.getContentElement().innerHTML += html;
	else		
		tab.notify();
	
	dp.SyntaxHighlighter.HighlightAll('code');
}

function setFilterLevel ( id, level )
{
	var tab = tabs.getTabById( id );
	
	if( tabs.tabUnderFocus == tab )
		filterController.setFilterLevel( level );
	else
		tab.getFile().setLevelFilter( level );
}
function setFilterMode ( id, mode )
{
	var tab = tabs.getTabById( id );
	
	if( tabs.tabUnderFocus == tab )
		filterController.setFilterMode( mode );
	else
		tab.getFile().setFilterMode( mode );
}

function setTabName ( id, name )
{
	tabs.getTabById( id ).setName( name );
}
function closeTab( id )
{
	tabs.removeTab( tabs.getTabById( id ) );
	dp.SyntaxHighlighter.HighlightAll('code');
}
function focusTab( id )
{
	tabs.setFocus( tabs.getTabById( id ) );
	dp.SyntaxHighlighter.HighlightAll('code');
}
function clearTab( id )
{
	if( tabs.getTabById( id )== tabs.tabUnderFocus )
		tabs.getContentElement().innerHTML = "";	
	
	tabs.getTabById( id ).getFile().setCache( "" );
}

function expandAll ()
{
	var a = getElementsByClassName( CSS.EXPANDABLE ) ;
	var l = a.length;
	
	//alert( a );
	//alert( l );
	
	for( var i = 0; i < l; i++ )
	{
		var el = a[ i ];

		if( !hasClass( el, CSS.OPENED ) )
		{
			if( el.onclick )
				el.onclick();
			else if( !exceptDetails )
				el.parentNode.onclick();
		}
	}
}
function collapseAll ()
{
	var a = getElementsByClassName( CSS.EXPANDABLE ) ;
	var l = a.length;
	
	//alert( a );
	//alert( l );
	
	for( var i = 0; i < l; i++ )
	{
		var el = a[ i ];

		if( hasClass( el, CSS.OPENED ) )
		{
			if( el.onclick )
				el.onclick();
			else if( !exceptDetails )
				el.parentNode.onclick();
			
			//alert( el.onclick );
		}
	}
}
function setExceptDetails ( b )
{
	exceptDetails = b;
}

function getElementsByClassName (strClass, strTag, objContElm) {
	strTag = strTag || "*";
	objContElm = objContElm || document;    
	var objColl = objContElm.getElementsByTagName(strTag);
	if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
	var arr = new Array();                              
	var delim = strClass.indexOf('|') != -1  ? '|' : ' ';   
	var arrClass = strClass.split(delim);    
	for (var i = 0, j = objColl.length; i < j; i++) {                         
		var arrObjClass = objColl[i].className.split(' ');   
		if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
		var c = 0;
		comparisonLoop:	
		for (var k = 0, l = arrObjClass.length; k < l; k++) {
			for (var m = 0, n = arrClass.length; m < n; m++) {
				if (arrClass[m] == arrObjClass[k]) c++;
				if ((delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
					arr.push(objColl[i]); 
					break comparisonLoop;
				}
			}
		}
	}
	return arr; 
}

// To cover IE 5 Mac lack of the push method
//Array.prototype.push = function(value) {this[this.length] = value; };
